﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YRevitJig;
using YJKRevitDimensionText.Forms;
using YJKRevitDimensionText.Utility;

namespace YJKRevitDimensionText.Jigs
{
	public class LeaderDimJig : XUiJigBase
	{
		public LeaderDimJig(UIDocument uiDoc, System.Windows.Forms.Form form) : base(uiDoc)
		{
			this.JigStage = LeaderDimJig.EStage.eNoOper;
			this.m_formLeaderDim = form;
			this.m_UserForm = form;
		}

		public XYZ PickStartPoint(string strPrompt = "请确定标注起点：")
		{
			try
			{
				this.JigStage = LeaderDimJig.EStage.eNoOper;
				this.StartPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.StartPoint = null;
			}
			return this.StartPoint;
		}

		public XYZ PickDimPoint(XYZ ptStart, string strPrompt = "请确定标注位置：")
		{
			try
			{
				this.JigStage = LeaderDimJig.EStage.eDimPosition;
				this.StartPoint = ptStart;
				this.DimPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				this.DimPoint = this.GetResetPoint(this.StartPoint, this.DimPoint);
			}
			catch
			{
				this.DimPoint = null;
			}
			this.JigStage = LeaderDimJig.EStage.eNoOper;
			return this.DimPoint;
		}

		public XYZ PickTextPoint(XYZ ptStart, XYZ ptDimPos, string strPrompt = "请确定标注方向：")
		{
			try
			{
				this.JigStage = LeaderDimJig.EStage.eTextPosition;
				this.StartPoint = ptStart;
				this.DimPoint = ptDimPos;
				this.TextPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				this.TextPoint = this.GetResetPoint(this.DimPoint, this.TextPoint);
			}
			catch
			{
				this.TextPoint = null;
			}
			this.JigStage = LeaderDimJig.EStage.eNoOper;
			return this.TextPoint;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (this.m_ptCur.IsEmpty)
			{
				return;
			}
			XYZ xyz = base.Client2Revit(this.m_ptCur);
			XGraphics xgraphics = new XGraphics(paintEventArgs.Graphics, base.JigPen);
			if (this.JigStage == LeaderDimJig.EStage.eDimPosition)
			{
				xyz = this.GetResetPoint(this.StartPoint, xyz);
				new XLine2D(base.Revit2Client(this.StartPoint), base.Revit2Client(xyz)).Draw(xgraphics);
				return;
			}
			if (this.JigStage == LeaderDimJig.EStage.eTextPosition)
			{
				DlgLeaderDim dlgLeaderDim = this.m_formLeaderDim as DlgLeaderDim;
				TextNoteType textNoteTypeByName = this.GetTextNoteTypeByName(base.UiDoc.Document, dlgLeaderDim.fontType);
				if (textNoteTypeByName == null)
				{
					return;
				}
				new XLine2D(base.Revit2Client(this.StartPoint), base.Revit2Client(this.DimPoint)).Draw(xgraphics);
				xyz = this.GetResetPoint(this.DimPoint, xyz);
				new XLine2D(base.Revit2Client(this.DimPoint), base.Revit2Client(xyz)).Draw(xgraphics);
				double num = textNoteTypeByName.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble() * (double)base.UiDoc.ActiveView.Scale;
				double num2 = textNoteTypeByName.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
				double num3 = num * num2 * RevitDocumentExtension.GetTextWidthFactory();
				double num4 = 1.2;
				List<string> list = SpecialCharactorDrawingKit.SplitString(dlgLeaderDim.LineUpText);
				list.Reverse();
				for (int i = 0; i < list.Count; i++)
				{
					double dBoxWidth = (double)SpecialCharactorDrawingKit.GetLength(list[i]) * num3 * num4;
					double num5 = num + num * (double)i * 1.35;
					XYZ ptLineStart = this.DimPoint + num5 * base.UiDoc.ActiveView.UpDirection;
					XYZ ptLineEnd = xyz + num5 * base.UiDoc.ActiveView.UpDirection;
					this.DrawTextBox(xgraphics, ptLineStart, ptLineEnd, dBoxWidth, num, dlgLeaderDim.alignStyle);
				}
				List<string> list2 = SpecialCharactorDrawingKit.SplitString(dlgLeaderDim.LineDownText);
				for (int j = 0; j < list2.Count; j++)
				{
					double dBoxWidth2 = (double)SpecialCharactorDrawingKit.GetLength(list2[j]) * num3 * num4;
					double num6 = -(num + num * (double)j * 1.35);
					XYZ ptLineStart2 = this.DimPoint + num6 * base.UiDoc.ActiveView.UpDirection;
					XYZ ptLineEnd2 = xyz + num6 * base.UiDoc.ActiveView.UpDirection;
					this.DrawTextBox(xgraphics, ptLineStart2, ptLineEnd2, dBoxWidth2, num, dlgLeaderDim.alignStyle);
				}
			}
		}

		private void DrawTextBox(XGraphics graphics, XYZ ptLineStart, XYZ ptLineEnd, double dBoxWidth, double dBoxHeight, string alignStyle)
		{
			Transform transform = Transform.CreateRotation(base.UiDoc.ActiveView.ViewDirection, Math.PI*.5);
			XYZ xyz = (ptLineEnd - ptLineStart).Normalize();
			XYZ xyz2 = transform.OfVector(xyz);
			XYZ xyz3;
			if (alignStyle == "末端对齐")
			{
				xyz3 = ptLineEnd - dBoxWidth * xyz;
			}
			else if (alignStyle == "居中对齐")
			{
				xyz3 = ptLineStart + 0.5 * (ptLineEnd - ptLineStart) - 0.5 * dBoxWidth * xyz;
			}
			else
			{
				xyz3 = ptLineStart;
			}
            //xyz3 + dBoxWidth * xyz;
			XYZ xyz4 = xyz3 + 0.5 * dBoxHeight * xyz2;
			XYZ xyz5 = xyz3 - 0.5 * dBoxHeight * xyz2;
			XYZ xyz6 = xyz4 + dBoxWidth * xyz;
			XYZ xyz7 = xyz5 + dBoxWidth * xyz;
			new XLine2D(base.Revit2Client(xyz4), base.Revit2Client(xyz6)).Draw(graphics);
			new XLine2D(base.Revit2Client(xyz5), base.Revit2Client(xyz7)).Draw(graphics);
			new XLine2D(base.Revit2Client(xyz5), base.Revit2Client(xyz4)).Draw(graphics);
			new XLine2D(base.Revit2Client(xyz7), base.Revit2Client(xyz6)).Draw(graphics);
		}

		private XYZ GetResetPoint(XYZ ptBase, XYZ ptUser)
		{
			XYZ result = ptUser;
			if (this.JigStage == LeaderDimJig.EStage.eDimPosition)
			{
				DlgLeaderDim dlgLeaderDim = this.m_formLeaderDim as DlgLeaderDim;
				if (dlgLeaderDim.ckbFixAngle)
				{
					double num = AssistFunc.AngleToRadian(Convert.ToDouble(dlgLeaderDim.ckbfixAngleSelect));
					XYZ projectPoint = JigFuncs.GetProjectPoint(base.UiDoc, ptUser, ptBase, num);
					XYZ projectPoint2 = JigFuncs.GetProjectPoint(base.UiDoc, ptUser, ptBase, -num);
					double num2 = ptUser.DistanceTo(projectPoint);
					double num3 = ptUser.DistanceTo(projectPoint2);
					result = ((num2 < num3) ? projectPoint : projectPoint2);
				}
			}
			else if (this.JigStage == LeaderDimJig.EStage.eTextPosition)
			{
				result = JigFuncs.GetProjectPoint(base.UiDoc, ptUser, ptBase, 0.0);
			}
			return result;
		}

		private TextNoteType GetTextNoteTypeByName(Autodesk.Revit.DB.Document doc, string name)
		{
			TextNoteType textNoteType = null;
			TextNoteType textNoteType2 = null;
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)))
			{
				TextNoteType textNoteType3 = element as TextNoteType;
				if (textNoteType3 != null)
				{
					if (textNoteType2 == null)
					{
						textNoteType2 = textNoteType3;
					}
					if (textNoteType3.Name.ToString().Equals(name))
					{
						textNoteType = textNoteType3;
						break;
					}
				}
			}
			if (textNoteType == null)
			{
				return textNoteType2;
			}
			return textNoteType;
		}

		public XYZ StartPoint { get; set; }

		public XYZ DimPoint { get; set; }

		public XYZ TextPoint { get; set; }

		public LeaderDimJig.EStage JigStage { get; set; }

		private System.Windows.Forms.Form m_formLeaderDim;

		public enum EStage
		{
			eNoOper,
			eDimPosition,
			eTextPosition
		}
	}
}
